<?php


namespace app\lunwen\controller;


use app\lunwen\model\StuTopicLogModel;
use app\lunwen\model\StuTopicModel;
use cmf\controller\HomeBaseController;

class StuSubmitTitleController extends HomeBaseController
{
    public function index(){
        $value = session('stu_name');
        $this->assign("stu_name", $value);
        return $this->fetch(':stuSubmitTitle');
    }
    public function submit(){
        $value = session('stu_name');
        $stuId = session('stu_id');

        $stutopiclog = new StuTopicLogModel();
        /*$stutopiclog->where('stu_id',$stuId)
                    ->where('status','1')
                    ->select();*/
        $www= $stutopiclog->where(['stu_id'=>$stuId,'status'=>'已提交'])->select();
        //var_dump ($www);

        if (count($www)!=0){
            $titleList = new StuTopicLogModel();
            $data = $titleList->where('stu_name',$value)->all();

            return json([
                'code'=>-1,
                'msg'=>'已经提交，不能重复提交',
                'data'=>$data
            ]);
        }

        $stutopic = new StuTopicModel();
        $stutopic->stu_name=$value;
        $stutopic->stu_title=$_POST["title"];
        $stutopic->stu_teacher=$_POST["teacher"];
        $stutopic->status='已提交';
        $stutopic->stu_id=$stuId;

        $stutopic->save();

        $stutopiclog2 = new StuTopicLogModel();
        $stutopiclog2->stu_name=$value;
        $stutopiclog2->title=$_POST["title"];
        $stutopiclog2->stu_teacher=$_POST["teacher"];
        $stutopiclog2->status='已提交';
        $stutopiclog2->stu_id=$stuId;

        $stutopiclog2->save();

        $titleList = new StuTopicLogModel();
        $data = $titleList->where('stu_name',$value)
            ->whereTime('create_time','>',session('beforeYear'))
            ->whereTime('create_time','<',session('afterYear'))
            ->all();

        return json([
            'code'=>1,
            'msg'=>'提交成功',
            'data'=>$data
        ]);
    }
}